
Imports Office = Microsoft.Office.Core
Imports Outlook = Microsoft.Office.Interop.Outlook

Public Class MAPISampleVB

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

        ' get the Outlook Application Object
        Dim outlookApplication As Outlook.Application = New Outlook.Application()

        ' get the namespace object
        Dim olNamespace As Outlook.NameSpace = outlookApplication.GetNamespace("MAPI")

        ' logon to Session, here we use an already opened Outlook
        olNamespace.Logon(, , True, True)

        ' create a new email and fill it with the infos provided in the Form
        Dim newMail As Outlook.MailItem = outlookApplication.CreateItem(Outlook.OlItemType.olMailItem)

        ' here we use our C++ library
        Dim fabric As MAPIConcubine.Fabric = New MAPIConcubine.Fabric()

        ' we pass the MAPIObject property to our library and the parameters
        ' note that in VB.Net you can't see the MAPIOBJECT property but be sure it is there
        fabric.AddMessageHeader(newMail.MAPIOBJECT, tbxHeaderName.Text, tbxHeaderValue.Text)

        ' that's all
        fabric = Nothing

        newMail.To = tbxRecipientAddress.Text
        newMail.Subject = tbxSubject.Text
        newMail.Body = tbxMessage.Text


        ' send out the email
        newMail.Send()
        newMail = Nothing

        ' logoff from namespace (session)
        olNamespace.Logoff()

        'release reference o outlook object
        outlookApplication = Nothing

        ' Let the Garbagecollector do his work 
        GC.Collect()
        GC.WaitForPendingFinalizers()

    End Sub

    Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
        Me.Close()
    End Sub
End Class
